<?php


namespace App\Admin\Controllers;


use Dcat\Admin\Traits\HasUploadedFile;
use Illuminate\Http\Request;

class FileController
{
    use HasUploadedFile;

    public function handle()
    {
        $disk = $this->disk();

        if ($this->isDeleteRequest()) {
            return $this->deleteFileAndResponse($disk);
        }

        $file = $this->file();

        $dir = 'images';
        $newName = date("YmdHis", time()) . '-' . uniqid() . '.' . $file->getClientOriginalExtension();

        $result = $disk->putFileAs($dir, $file, $newName);

        $path = "{$dir}/$newName";

        return $result
            ? $this->responseUploaded($path, $disk->url($path))
            : $this->responseErrorMessage('文件上传失败');
    }

    public function payCert(Request $request)
    {
        $disk = $this->disk('local');

        if ($this->isDeleteRequest()) {
            return $this->responseDeleted();
        }

        $file = $this->file();

        $dir = 'cert';

        $newName = date("YmdHis", time()) . '-' . uniqid() . '.' . $file->getClientOriginalExtension();

        $result = $disk->putFileAs($dir, $file, $newName);

        $path = "{$dir}/$newName";

        return $result
            ? $this->responseUploaded($path, $disk->url($path))
            : $this->responseErrorMessage('文件上传失败');
    }
}
